package office.service.workflow.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.workflow.form.WorkTaskForm;
import office.action.workflow.wrapper.WorkTaskWrapper;
import office.bo.workflow.impl.WorkTask;
import office.domainset.BizDomainProviderSetImpl;
import office.service.workflow.IWorkTaskService;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class WorkTaskServiceImpl extends BaseBizServiceImpl implements IWorkTaskService{
	
	public void editWorkTask(WorkTaskForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			WorkTask bo = (WorkTask)domainProviderSetImpl.getWorkTaskDP().newEmptyObj();
			bo =(WorkTask) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑待办任务失败!", e);
		}
	}
	public WorkTaskWrapper getById(String id)throws BusinessException{
		WorkTaskWrapper form = new WorkTaskWrapper();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			WorkTask bo = (WorkTask)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getWorkTaskDP(), id);
			form.copyToWrapper(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取待办任务失败!", e);
		}
		return form;
	}
	public List<WorkTaskWrapper> findWorkTaskByCondition(WorkTaskForm condition)throws BusinessException{
		List<WorkTaskWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getWorkTaskDP();
			Map condMap= new HashMap();
			if(null!=condition){
				if(!Util.isNull(condition.getId())){
					condMap.put("id", condition.getId());
				}
				if(!Util.isNull(condition.getName())){
					condMap.put("name", condition.getId());
				}
				if(!Util.isNull(condition.getObjId())){
					condMap.put("objId", condition.getObjId());
				}
				if(!Util.isNull(condition.getObjClassName())){
					condMap.put("objClassName", condition.getObjClassName());
				}
				if(!Util.isNull(condition.getSubmitUser())){
					condMap.put("submitUser", condition.getSubmitUser());
				}
				if(!Util.isNull(condition.getHandleUser())){
					condMap.put("handleUser", condition.getHandleUser());
				}
				if(!Util.isNull(condition.getHandleState())){
					condMap.put("handleState", condition.getHandleState());
				}
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findWorkTaskByCondition",condMap,new HashMap());
			WorkTaskWrapper wrapper;
			for(int i=0;i<list.size();i++){
				WorkTask bo = (WorkTask)list.get(i);
				wrapper = new WorkTaskWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取待办任务列表失败！", e);
		}
		return wrapperList;
	}
}
